﻿package zf.display.tools
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFormat;

	/**
	 * 附加码变化事件 
	 * @eventType flash.events.Event.CHANGE
	 */
	[Event(name="change", type="flash.events.Event")]

	/**
	 * 附加码类。可以生成一个指定位数和字符集的附加码。 
	 * @author mike.zhang
	 * @langversion 3.0 
	 * @playerversion Flash 9 
	 */
	public class AddCode extends Sprite
	{
		private var _codes:String;
		private var _code:String;
		private var _length:uint=4;
		private var _bmp:Bitmap;

		/**
		 * 创建一个附加码 
		 * @param l 附加码字符个数
		 * @param s 附加码字符集
		 * 
		 */
		public function AddCode (l:uint=4,s:String="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"):void
		{
			_codes=s;
			_length=l;
			create ();
		}
		/**
		 * 附加码字符个数，默认为4 
		 * @return 
		 * 
		 */
		public function get length ():uint
		{
			return _length;
		}
		public function set length (v:uint):void
		{
			_length=v;
		}
		/**
		 * 附加码字符集，默认为数字和大小写字母 
		 * @return 
		 * 
		 */
		public function get codes ():String
		{
			return _codes;
		}
		public function set codes (v:String):void
		{
			_codes=v;
		}
		/**
		 * 检测字符串是否和附加码相同，若设置 ignore 为true则忽略大小写
		 * @param str 待检测的字符串
		 * @param ignore 是否忽略大小写
		 * @return 字符串是否和附加码相同
		 * 
		 */
		public function check (str:String,ignore:Boolean=true):Boolean
		{
			if (ignore)
			{
				return str.toLowerCase() == _code.toLowerCase();
			}
			return str == _code;
		}
		/**
		 * 创建附加码 
		 * 
		 */
		public function create ():void
		{
			try
			{
				removeChild (_bmp);
			}
			catch (e:Error)
			{
			}
			_code=createRandomString(_length);
			_bmp=createAddCode(_code);
			addChild (_bmp);
			addEventListener (MouseEvent.CLICK,clickEvent);
		}
		private function clickEvent (e:MouseEvent):void
		{
			create ();
			dispatchEvent (new Event(Event.CHANGE));
		}
		/**
		 * 创建附加码字符串 
		 * @param num 字符串长度
		 * @return 字符串
		 * 
		 */
		private function createRandomString (num:uint):String
		{
			//var arr:Array=new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
			var str:String=_codes;
			var ss:String="";
			//var l:uint=arr.length;
			var l:uint=str.length;
			for (var i:uint=0; i < num; i++)
			{
				var index:uint=Math.floor(Math.random() * 99999) % l;
				//ss+= arr[index];
				ss+=str.charAt(index);
			}
			return ss;
		}
		/**
		 * 创建附加码图形 
		 * @param ss 附加码显示字符串
		 * @return Bitmap
		 * 
		 */
		private function createAddCode (ss:String):Bitmap
		{
			var l:uint=ss.length;
			var sprite:Sprite=new Sprite;

			var sp:Sprite=new Sprite;

			sprite.addChild (sp);
			var ww:Number=5;
			var hh:Number=20;

			var tf:TextField=new TextField;

			tf.text=ss;
			tf.antiAliasType=AntiAliasType.ADVANCED;
			tf.sharpness=400;
			var format:TextFormat = new TextFormat();
			format.bold=true;
			format.color = 0xFF0000;
			format.size = 14;
			format.letterSpacing=2;
			tf.setTextFormat (format);
			tf.x=ww;
			tf.y=(hh-tf.textHeight)/2-2;
			tf.width=tf.textWidth+5;
			tf.height=tf.textHeight+2;

			ww+=tf.textWidth+7;
			sprite.addChild (tf);


			var rr:int=Math.floor(Math.random()*128);
			var gg:int=Math.floor(Math.random()*255);
			var bb:int=Math.floor(Math.random()*255);
			var color:ColorTransform=new ColorTransform(1,1,1,1,rr,gg,bb,1);
			sp.graphics.beginFill (color.color);
			sp.graphics.drawRect (0,0,ww,hh);

			var bitMap:BitmapData=new BitmapData(sprite.width,sprite.height);//建立位图数据
			bitMap.draw (sprite);//获取位图
			var bmp:Bitmap = new Bitmap(bitMap);//还原位图

			return bmp;
		}
	}
}